package com.zhsj.OkeHomeConsumption.utils;

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class CodeUtils {

    public static  String password = "okejiaphoneAESPW";
    /**
     * 解密算法/工作模式/填充方式
     * <p>
     * JAVA6 支持PKCS5PADDING填充方式
     * Bouncy castle支持PKCS7Padding填充方式
     */
    public static final String CIPHER_ALGORITHM = "AES/ECB/PKCS7Padding";
    public static final String CIPHER_5ALGORITHM = "AES/ECB/PKCS5Padding";

    /**
     * 生成密钥
     */
    public static Key getKey(String password) {
        byte[] keybyte = new byte[]{0x08, 0x08, 0x04, 0x0b, 0x02, 0x0f, 0x0b, 0x0c,
                0x01, 0x03, 0x09, 0x07, 0x0c, 0x03, 0x07, 0x0a, 0x04, 0x0f,
                0x06, 0x0f, 0x0e, 0x09, 0x05, 0x01, 0x0a, 0x0a, 0x01, 0x09,
                0x06, 0x07, 0x09, 0x0d};
        SecretKeySpec key = new SecretKeySpec(keybyte, password);
        return key;
    }

    /**
     * aes解密
     *
     * @param content  待解密内容
     * @param password 解密密钥
     * @return
     */
    public static String aesDecrypt(String content, String password) {
        try {
            if (StringUtils.isEmpty(password)) {
                password = CodeUtils.password;
            }
            Key key = getKey(password);
            /**
             * 实例化 
             * 使用 PKCS7PADDING 填充方式，按如下方式实现,就是调用bouncycastle组件实现 
             * Cipher.getInstance(CIPHER_ALGORITHM,"BC") 
             */
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
            byte[] result = cipher.doFinal(parseHexStr2Byte(content));
            return new String(result); // 加密
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将二进制转换成16进制
     *
     * @param buf
     * @return
     */
    public static String parseByte2HexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    /**
     * 将16进制转换为二进制
     *
     * @param hexStr
     * @return
     */
    public static byte[] parseHexStr2Byte(String hexStr) throws NumberFormatException {
        if (hexStr.length() < 1)
            return null;
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }

    /**
     * AES加密
     *
     * @param content 需要加密的内容
     * @param key     加密密码
     * @return
     */
    public static String encryptAES(String content, String key) {
        byte[] byteContent = new byte[0]; // 避免空指针，设置不占用控件的byte[]
        byte[] encryptedBytes = new byte[0];
        try {
            byteContent = content.getBytes("UTF-8");
            byte[] enCodeFormat = key.getBytes();
            SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");

            // byte[] initParam = IV_STRING.getBytes();
            // IV_STRING AES指定偏移量，可以设置，可以不设置，根据加密模式确定

            // IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
            // 设置偏移量

            // 指定加密的算法、工作模式和填充方式
            Cipher cipher = null;
            cipher = Cipher.getInstance(CIPHER_5ALGORITHM);
            //CIPHER_5ALGORITHM =AES/ECB/PKCS5Padding   AES 加密方式，ECB 模式 ， PKCS5Padding 填充方式；

            //cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec,ivParameterSpec);
            //cipher.init方法，可以谁知传入偏移量，或者不传入

            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
            encryptedBytes = cipher.doFinal(byteContent);
            //最终加密后输入的是 bety【】 数组，这里服务端需要什么格式，直接转换就可以
        } catch (Exception e) {
        }
        return bytes2HexString(encryptedBytes);
    }

    // btye[] 转 hex
    public static String bytes2HexString(byte[] b) {
        String ret = "";
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            ret += hex;
        }
        return ret;
    }
}
